package com.longrise.co.api;

import android.content.Context;

import com.longrise.co.ui.login.model.LoginBean;
import com.longrise.co.utils.Constants;
import com.ly.common.commonutils.SPUtils;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.security.EnCryptDeCrypt2;

/**
 * Created by luoyang on 2017-7-11.
 */

public class CommonPara {

    private static CommonPara instance;

    private String user = "chezhutong";

    private String userFlag ;

    private String enUserFlag;

    private LoginBean loginBean;

    private String areaId ;

    private String enAreaId ;

    private Context mContext;

    private String mUserId;

    //记录当前城市 默认郑州
    private String city;

    private boolean authStatus = false;

    private boolean isShowKckpSet = false;


    private String kckpUrl ;

    private String latitude;//纬度
    private String longitude;//经度
    private String userAreaid;// 用户信息中的区域id
    private String defaultAreaid;//默认的区域id;

    public boolean isModifyPwd() {
        return modifyPwd;
    }

    public void setModifyPwd(boolean modifyPwd) {
        this.modifyPwd = modifyPwd;
    }

    private boolean modifyPwd = false;

    public boolean isAuthStatus() {
        return authStatus;
    }

    public boolean isShowKckpSet() {
        return isShowKckpSet;
    }

    public void setShowKckpSet(boolean showKckpSet) {
        isShowKckpSet = showKckpSet;
    }

    public void setAuthStatus(boolean authStatus) {
        this.authStatus = authStatus;
    }

    public static CommonPara getInstance() {
        if(instance == null) {
            synchronized (ApiService.class) {
                if(instance == null) {
                    instance = new CommonPara();
                }
            }
        }
        return instance;
    }

    private CommonPara(){}

    public void init(Context context) {
        this.mContext = context;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getUserFlag() {
        if(StringUtils.isEmpty(userFlag)) {
            userFlag = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_USERFLAG);
        }
        return userFlag;
    }

    public void setUserFlag(String userFlag) {
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERFLAG, userFlag);
        this.userFlag = userFlag;
    }

    public String getEnUserFlag() {
        if(StringUtils.isEmpty(enUserFlag)) {
            enUserFlag = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_ENUSERFLAG);
        }
        return enUserFlag;
    }

    public void setEnUserFlag(String enUserFlag) {
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_ENUSERFLAG, enUserFlag);
        this.enUserFlag = enUserFlag;
    }

    public String getCity() {
        if(StringUtils.isEmpty(city)) {
            city = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_CITY);
//            if(StringUtils.isEmpty(city)) {
//                city = Constants.DEFAULT_CITY;
//            }
        }
        return city;
    }

    public void setCity(String city) {
        this.city = city;
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_CITY, city);
    }


    public LoginBean getLoginBean() {
        if(loginBean == null) {
            loginBean = new LoginBean();
            loginBean.setAuthstatus(SPUtils.getSharedStringData(mContext, Constants.SP_KEY_AUTHSTATUS));
            loginBean.setAreaid(SPUtils.getSharedStringData(mContext, Constants.SP_KEY_AREAID));
            loginBean.setMobile(SPUtils.getSharedStringData(mContext, Constants.SP_KEY_MOBILE));
            loginBean.setPhoto(SPUtils.getSharedStringData(mContext, Constants.SP_KEY_PHOTO));
            loginBean.setUserflag(SPUtils.getSharedStringData(mContext, Constants.SP_KEY_USERFLAG));
            loginBean.setUsertype(SPUtils.getSharedStringData(mContext, Constants.SP_KEY_USERTYPE));
        }
        return loginBean;
    }

    public void setLoginBean(LoginBean bean) {
        this.loginBean = bean;
        if(bean == null) {
            return ;
        }
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_AUTHSTATUS, bean.getAuthstatus());
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_AREAID, bean.getAreaid());
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_MOBILE, bean.getMobile());
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_PHOTO, bean.getPhoto());
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERFLAG, bean.getUserflag());
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERTYPE, bean.getUsertype());
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERID, bean.getUserid());
    }

    public String getAreaId() {
        if(StringUtils.isEmpty(areaId)) {
            areaId = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_AREAID);
            if(StringUtils.isEmpty(areaId)) {
                areaId = Constants.DEFAULT_AREAID;
            }
        }
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_AREAID, areaId);
    }

    public String getEnAreaId() {
        try {
            areaId = getAreaId();
            enAreaId = EnCryptDeCrypt2.getInstance().EnCrypt(areaId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return enAreaId;
    }

    public String getmUserId() {
        if(StringUtils.isEmpty(mUserId)) {
            mUserId = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_USERID);
        }
        return mUserId;
    }

    public void setmUserId(String mUserId) {
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERID, mUserId);
        this.mUserId = mUserId;
    }

    public void clear() {
        userFlag = null;
        enUserFlag = null;
        loginBean = null;
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_AUTHSTATUS, null);
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_AREAID, null);
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_MOBILE, null);
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_PHOTO, null);
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERFLAG, null);
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERTYPE, null);
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_USERID, null);
    }

    public boolean isLogin() {
        String userFlag = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_USERFLAG);
        if(StringUtils.isEmpty(userFlag)) {
            return false;
        }
        return true;
    }

    public String getKckpUrl() {
        if(StringUtils.isEmpty(kckpUrl)) {
            kckpUrl = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_KCKP_URL);
            if(StringUtils.isEmpty(kckpUrl)) {
                kckpUrl = ApiConstants.KCKPURL;
            }
        }
        return kckpUrl;
    }

    public void setKckpUrl(String kckpUrl) {
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_KCKP_URL, kckpUrl);
        this.kckpUrl = kckpUrl;
    }
    public String getLatitude() {
        if(StringUtils.isEmpty(latitude)){
            latitude = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_LATITUDE);
        }
        return latitude;
    }

    public void setLatitude(String latitude) {
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_LATITUDE, latitude);
        this.latitude = latitude;
    }

    public String getLongitude() {
        if(StringUtils.isEmpty(longitude)){
            longitude = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_LONGITUDE);
        }
        return longitude;
    }

    public void setLongitude(String longitude) {
        SPUtils.setSharedStringData(mContext, Constants.SP_KEY_LONGITUDE, longitude);
        this.longitude = longitude;
    }

    public void setUserAreaid(String userAreaid){
        SPUtils.setSharedStringData(mContext,Constants.SP_KEY_USERAREAID,userAreaid);
        this.userAreaid = userAreaid;
    }

    public String getUserAreaid(){
        if(StringUtils.isEmpty(userAreaid)){
            userAreaid = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_USERAREAID);
        }
        return userAreaid;
    }

    public void setDefaultAreaid(String defaultAreaid){
        SPUtils.getSharedStringData(mContext,Constants.SP_KEY_DEFAULTAREAID);
        this.defaultAreaid = defaultAreaid;
    }

    public String getDefaultAreaid(){
        if(StringUtils.isEmpty(defaultAreaid)){
            defaultAreaid = SPUtils.getSharedStringData(mContext, Constants.SP_KEY_DEFAULTAREAID);
        }
        return defaultAreaid;
    }
}
